<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use menus\Menus;
class Menu extends \app\index\controller\Common
{
    public function get_left_nav()
    {
        $menu = new \menus\Menus();//加载外部类
        $list=$menu->get_list(Session::get('user.uid'));
        $this->result($list);
    }
    //添加修改 获取菜单角色表
    public function menu_group()
	{
		$map = array();
		$map['status']='1';
        $data=Db::table('menu_group')->where($map)->select();
        $this->result($data);
	}

     public function node_list()
    {
        $id=Request::instance()->get('id');
        $page=Request::instance()->get('page');
        $limit=Request::instance()->get('limit');
        //搜索条件
        $name=Request::instance()->get('name');
        $title=Request::instance()->get('title');
        $pid=Request::instance()->get('pid');
        //$type=Request::instance()->get('type');
        $map = array();
        $data = array();
        if ($name!="") {
            $map['name'] = ['like',"%$name%"];
        }
        if ($title!="") {
            $map['title'] = ['like',"%$title%"];
        }
        if ($pid!="") {
            $map['pid'] = $pid;
        }
        //$data=Db::table('menu_list')->where($map)->page("$page,$limit")->select();
        $data=Db::table('menu_list')->where($map)->select();
        foreach ($data as $key => $value) {
            $data[$key]['lay_icon_open']=$value['icon'];
            $data[$key]['lay_icon_close']=$value['icon'];
            $data[$key]['lay_icon']=$value['icon'];
        }
        //拥有权限的用户$data['lay_is_checked']=true;
        if ($id && $id!='undefined') {
            $menu_group=Db::table('menu_group')->where('id',$id)->find();
            $rules=explode(',', $menu_group['rules']);
            foreach ($data as $key => $value) {
                foreach ($rules as $k => $v) {
                    if ($v==$value['id']) {
                        $data[$key]['lay_is_checked']=true;
                    }
                }
            }
        }
        $arr['code']='0';
        $arr['msg']='';
        $arr['count']=Db::table('menu_list')->count();
        $arr['data']=$data;
        $arr['is']=true;
        $arr['tip']='操作成功！';
        return $arr;
    }
    public function add()
    {
        $data = array();
        $data['pid']=Request::instance()->post('pid');
        $data['name']='get';//Request::instance()->post('name');
        $data['title']='新建菜单';//Request::instance()->post('title');
        $data['spread']='0';//Request::instance()->post('type');
        if (Request::instance()->has('status','post')) {
            $data['status']=Request::instance()->post('status');
        }else{
            $data['status']='0';
        }
        Db::table('menu_list')->insert($data);
        $id = Db::name('menu_list')->getLastInsID();
        $data=Db::table('menu_list')->where('id',$id)->find();
        $this->result($data,0,'提交成功');
    }
    public function edit()
    {
        
        if (Request::instance()->has('id','post')) {
            $id=Request::instance()->post('id');

            $data = array();
            $data['name']=Request::instance()->post('name');
            $data['title']=Request::instance()->post('title');
            $data['icon']=Request::instance()->post('icon');
            $data['jump']=Request::instance()->post('jump');
            if (Request::instance()->has('spread','post')) {
                $data['spread']=Request::instance()->post('spread');
            }else{
                $data['spread']='0';
            }
            if (Request::instance()->has('status','post')) {
                $data['status']=Request::instance()->post('status');
            }else{
                $data['status']='0';
            }
            Db::table('menu_list')->where('id',$id)->update($data);
            $this->result($id,0,'提交成功');
        }
    }
    //三级以上不会删除
    public function del()
    {
        if (Request::instance()->has('id','post')) {
            $id=Request::instance()->post('id');
            if (Db::table('menu_list')->delete($id)) {
                Db::table('menu_list')->where('pid',$id)->delete();
                $this->result($id,0,'提交成功');
            }else{
                $this->result($id,1,'删除失败');
            }
        }
    }






}
